//
//  NGMessageViewController.m
//  NetMobileLocation
//
//  Created by Mehmet Sinan Şahin on 2/9/12.
//  Copyright (c) 2012 netgis. All rights reserved.
//

#import "NGMessageViewController.h"
#import "SetLocationResponse.h"
#import "NGUtility.h"
#import "SetLocationsResponse.h"
#import "NGDBHelper.h"
#import "NGAppDelegate.h"
#import <QuartzCore/QuartzCore.h>


@implementation NGMessageViewController {
    
}

@synthesize message;
@synthesize toMessagesButton;
@synthesize navItem;
@synthesize lblTime;
@synthesize lblMessageBody;
@synthesize messageBodyTextView;
@synthesize imgMessagaType;
@synthesize scrollView;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        nibNameOrNil = [nibNameOrNil stringByAppendingString:@"-ipad"];
    }
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UIButton* backButton = [UIButton buttonWithType:101];
        [backButton setTitle:TTLocalizedString(@"Messages", @"") forState:UIControlStateNormal];
        [backButton addTarget:self 
                       action:@selector(toMessages:) 
             forControlEvents:UIControlEventTouchUpInside];
        scrollView.layer.cornerRadius = 10.0f;
        toMessagesButton = [[UIBarButtonItem alloc] initWithCustomView: backButton];

    }
    return self;
}

-(IBAction)toMessages:(id)sender {
    [self.view removeFromSuperview];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]]];

    [navItem setLeftBarButtonItem:toMessagesButton animated:false];
    [imgMessageType setImage:[NGUtility imageForMessageType:[message getType]]];
    [navItem setTitle:[message viewMessageHeader]];
    [lblMessageBody setText:[message viewMessageBody]];
    lblMessageBody.numberOfLines = 0; //will wrap text in new line
    [lblMessageBody sizeToFit];
    [lblTime setText:message.Time];
    CGRect rectLblTime = CGRectMake(45, 20 + lblMessageBody.bounds.size.height, 250, 20);
    
    lblTime.frame = rectLblTime;
    [scrollView.layer setCornerRadius:10.0f];
    [scrollView.layer setMasksToBounds:YES];
    
    if (!message.read) {
        [[NGDBHelper openDatabase] markMessageAsRead:message._id];
        long newMessageNumber =[[NGDBHelper openDatabase] howManyNewMessages]; 
        [[NGAppDelegate getApp] setMessageBadge:newMessageNumber];
    }
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [toMessagesButton release];
    [lblMessageBody release];
    [scrollView release];
    [navItem release];
    [message release];
}

@end
